<template>
  <div class="app-container calendar-list-container">
    <div v-if="visible">
      <el-form :model="entity" :rules="dataRule" ref="entity" label-width="100px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="权限名称" prop="positionName">
              <el-input v-model="entity.positionName" :disabled="view"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="数据类型" prop="dataAuth">
              <el-select v-model="entity.dataAuth" :disabled="view" placeholder="数据类型">
                <el-option v-for="(type,index) in dataAuth" :label="type.name" :value="type.value" :key="index"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="非超管可见" prop="useble">
              <el-select v-model="entity.useble" :disabled="view" placeholder="非超管可见">
                <el-option v-for="(type,index) in yesOrNo" :label="type.name" :value="type.value" :key="index"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <div class="dialog-footer-div">
          <el-button @click="cancel()">取消</el-button>
          <el-button v-if="!view" type="primary" @click="submit()">确定</el-button>
        </div>
      </span>
    </div>
  </div>
</template>

<script>
import request from '@/router/axios'
import { Message, MessageBox } from 'element-ui'
import url from '@/const/apiURL'
import { dict } from '@/const/dict'

export default {
  data () {
    return {
      view: false,
      url: '',
      yesOrNo: [],
      dataAuth: [],
      entity: {},
      dataRule: {
        positionName: [
          { required: true, message: '权限名称不能为空', trigger: 'blur' },
          { min: 2, message: '长度在不能小于2', trigger: 'blur' }
        ],
        dataAuth: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        useble: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.yesOrNo = dict().YesOrNo
    this.dataAuth = dict().DataAuth
  },
  props: {
    visible: {
      type: Boolean,
      default: function () {
        return false
      }
    }
  },
  methods: {
    init (title, entity, view, url) {
      this.$nextTick(() => {
        this.title = title
        this.$refs['entity'].resetFields()
        this.view = view
        this.entity = entity
        this.url = url
      })
    },
    cancel () {
      this.$emit('cancel')
    },
    submit () {
      this.$refs['entity'].validate((valid) => {
        if (valid) {
          this.handleUrl(this.entity).then((data) => {
            if (data) {
              this.$emit('cancel')
            }
          })
        }
      })
    },
    handleUrl (data) {
      data.appCode = this.appCode
      return request({
        url: this.url,
        method: 'post',
        data
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
</style>
